#ifndef STRUSTDEFS
#define STRUSTDEFS
#ifndef _GLIBCXX_IOSTREAM
#include <iostream>
#endif
// 二叉树
typedef struct binode
{
    int data;
    struct binode *lchild;
    struct binode *rchild;
} binode, *bitree;
// 单链表
typedef struct lnode
{
    int data;
    struct lnode *next;
} lnode, *linklist;

// 打印链表
void printlist(linklist l)
{
    lnode *p = l->next;
    while (p != nullptr)
    {
        std::cout << p->data << " ";
        p = p->next;
    }
    std::cout << std::endl;
}
// 尾插法创建链表
void makelist(linklist &l, int a[], int length)
{
    l = new lnode;
    l->next = nullptr;
    lnode *r = l, *s;
    for (int i = 0; i < length; i++)
    {
        s = new lnode;
        s->data = a[i];
        s->next = nullptr;
        r->next = s;
        r = s;
    }
    r = nullptr;
}
// 创建图的邻接表

typedef char vertexType; //顶点中存字符类型数据

/*边表节点*/
typedef struct ArcNode
{
    int adjvex;           //结点下标
    struct ArcNode *next; //指向后继节点
} ArcNode;

/*顶点表节点*/
typedef struct VNode
{
    vertexType data;
    ArcNode *first; //指向第一个边表节点
} VNode, AdjList[100];

/*图*/
typedef struct
{
    AdjList vertices;
    int vecnum, arcnum;
} ALGraph;

void create(ALGraph *G)
{
    std::cout << "-----------------------------------------------------" << std::endl;
    std::cout << "请输入顶点数和边数" << std::endl;
    std::cin >> G->vecnum >> G->arcnum;
    std::cout << "请输入顶点" << std::endl;

    for (int i = 0; i < G->vecnum; i++)
    {
        std::cin >> G->vertices[i].data;
        G->vertices[i].first = NULL;
    }

    std::cout << "请输入边（Vi,Vj）的下标i,j" << std::endl;
    int i, j;
    ArcNode *p;
    for (int k = 0; k < G->arcnum; k++)
    {
        std::cin >> i >> j;
        p = (ArcNode *)malloc(sizeof(ArcNode)); //创建新的边表结点
        p->adjvex = j;
        p->next = G->vertices[i].first; //头插法
        G->vertices[i].first = p;

        /*如果是无向图还应该加入下面这一段*/
        p = (ArcNode *)malloc(sizeof(ArcNode));
        p->adjvex = i;
        p->next = G->vertices[j].first;
        G->vertices[j].first = p;
    }

    std::cout << "邻接表如下：" << std::endl;
    for (int i = 0; i < G->vecnum; i++)
    {
        p = G->vertices[i].first;
        while (p)
        {
            std::cout << "<" << G->vertices[i].data << "," << G->vertices[p->adjvex].data << "> ";
            p = p->next;
        }
        std::cout << std::endl;
    }
    std::cout << "-----------------------------------------------------" << std::endl;
}

#endif //STRUSTDEFS
